
const Constant = require("./constant.js");
const PageTag = require("./momoGhsPageTagV2.js");
// const PageUtil = require("./pageUtil.js");
// const RunDefine = require("./runDefine.js");



// 操作 打开 send money 页
const doOpenSendMoneyPage = () => {
    // 点击 Send money
    console.log(Constant.UI_SDK_VERSION + "开始操作打开 Send money 页");
    sleep(500);
    click("Send money");
    sleep(500);
    click("Send money");
    return true;
}

// 操作 打开 send money 页
const doOpenMtnMoMoPage = () => {
    // 点击 Send money
    console.log(Constant.UI_SDK_VERSION + "开始操作打开 To MTN MoMo 页");
    sleep(500);
    click("To MTN MoMo")
    // sleep(500);
    // click("To MTN MoMo")
    return true;
}


// 操作 mtn account input
const doMtnMoMoAccountInput = (targetMtn, targetAmount, targetReference) => {
    let editList = className("EditText").find();
    console.log(Constant.UI_SDK_VERSION + "开始操作 mtn momo account input");
    console.log(Constant.UI_SDK_VERSION + "targetMtn" + targetMtn);
    console.log(Constant.UI_SDK_VERSION + "targetAmount" + targetAmount);
    console.log(Constant.UI_SDK_VERSION + "targetReference" + targetReference);

    if (targetMtn.length > 0 && targetAmount.length > 0) {

        let findTag1 = false;
        let findTag2 = false;
        let findTag3 = false;

        let findTagSend = false;

        for (let index = 0; index < editList.length; index++) {
            let tv = editList[index];
            let curText = tv.text() || ""
            // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: " + curText)

            if (curText.indexOf(PageTag.sendMoneyMtnAccountInputPageHintTag1) !== -1) {
                console.log(Constant.UI_SDK_VERSION + "设置 mtn account input number: " + targetMtn);
                tv.setText(targetMtn);
                findTag1 = true;
            }
            if (curText.indexOf(PageTag.sendMoneyMtnAccountInputPageHintTag2) !== -1) {
                console.log(Constant.UI_SDK_VERSION + "设置 mtn account input amount: " + targetAmount);
                tv.setText(targetAmount);
                findTag2 = true;
            }
            if (curText.indexOf(PageTag.sendMoneyMtnAccountInputPageHintTag3) !== -1) {
                console.log(Constant.UI_SDK_VERSION + "设置 mtn account input amount: " + targetReference);
                tv.setText(targetReference);
                findTag3 = true;
            }

            if (curText.indexOf("send") !== -1) {
                console.log(Constant.UI_SDK_VERSION + "找到 send 按键");
                findTagSend = true;
            }
        }

        sleep(2 * 1000)

        // 注意，找不到 send ，只能通过 bounds 来触发

        if (findTag1 && findTag2 && findTag3) {
            console.log(Constant.UI_SDK_VERSION + "点击 关闭键盘");
            // back();
            // bounds(0, 1590, 10, 1613).clickable().click()
            click(10, 1600);

            sleep(2 * 1000)

            console.log(Constant.UI_SDK_VERSION + "点击 确认");
            // click("Send");
            //bounds(36, 1518, 684, 1613).clickable().click()

            if (findTagSend) {
                console.log(Constant.UI_SDK_VERSION + "找到 send 按键");

                click("Send");
                sleep(500)
                click("Send");
                sleep(500)
                click("Send");
                sleep(500)
            } else {
                console.log(Constant.UI_SDK_VERSION + "找不到 send 按键");

                let gx = device.width / 2;
                let gy = device.height - 40;
                console.log(gx + ", " + gy);
                click(gx, gy);
                sleep(500);
            }

            editList.length = 0;
            editList = null;
            return true;
        }
    }

    editList.length = 0;
    editList = null;
    console.log(Constant.UI_SDK_VERSION + "操作登录结束: 当前不是 mtn momo account input 页")
    return false;
}

// 操作 点击 Complete 转账
const doClickTransferComleted = () => {
    // 点击 Send money
    console.log(Constant.UI_SDK_VERSION + "开始操作点击 Complete");
    sleep(500);
    click("Complete");
    sleep(500);
    click("Complete");
    return true;
}

// 操作 点击 Done 结束返回
const doClickTransferDoneAndGoBack = () => {
    // 点击 back
    console.log(Constant.UI_SDK_VERSION + "开始操作点击 Done");
    click("Done");
    sleep(500);
    click("Done");
    return true;
}

module.exports = {
    doOpenSendMoneyPage: doOpenSendMoneyPage,
    doOpenMtnMoMoPage: doOpenMtnMoMoPage,
    doMtnMoMoAccountInput: doMtnMoMoAccountInput,
    doClickTransferComleted: doClickTransferComleted,
    doClickTransferDoneAndGoBack: doClickTransferDoneAndGoBack
};